﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;



namespace CSKB
{
    public partial class CSKB : Form
    {
        bool m_bRunByUser;
        int m_iTargetPid;
        Thread m_playThread;
        KeyBoardPlayer m_kbPlayer;

        public string EkbFilePath
        {
            get { return txbxEkbFile.Text;}
            set { txbxEkbFile.Text = value; }
        }

        public int TargetPid
        {
            get { return m_iTargetPid; }
            set { m_iTargetPid = value; }
        }

        public bool RunByUser
        {
            get { return m_bRunByUser; }
            set { m_bRunByUser = value; }
        }

        public CheckBox CkbCalc
        {
            get { return ckbCalc; }
        }

        public CheckBox CkbNote
        {
            get { return ckbNote; }
        }

        public CheckBox CkbHtml
        {
            get { return cbxHtml; }
        }

        public CSKB(bool bRunByUser)
        {
            InitializeComponent();
            m_iTargetPid = 0;
            m_bRunByUser = bRunByUser;
            m_kbPlayer = null;
            DateTime dt = System.DateTime.Now;
            if (dt.Hour >= 15 || (dt.Hour < 9) || dt.DayOfWeek == DayOfWeek.Sunday || dt.DayOfWeek == DayOfWeek.Saturday)
            {
                combxCheckFile.SelectedIndex = 0;
            }
            else
            {
                combxCheckFile.SelectedIndex = 1;
            }            
        }

        private void EnableControls(bool bEnable)
        {
            ckbCalc.Enabled = bEnable;
            ckbNote.Enabled = bEnable;
            txbxEkbFile.Enabled = bEnable;
            btnEkbFile.Enabled = bEnable;
            btnStop.Enabled = !bEnable;
            cbxHtml.Enabled = bEnable;
            combxCheckFile.Enabled = bEnable;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (!btnStart.Checked && m_kbPlayer != null)
            {               
                Monitor.Exit(m_kbPlayer);
            }
            else
            {
                btnStart.Checked = false;
            }

            m_playThread.Abort();
            m_playThread.Join();
            if (m_kbPlayer.m_sw != null)
            {
                m_kbPlayer.m_sw.Close();
                m_kbPlayer.m_sw = null;
            }

            btnStart.Text = "Start";
            m_playThread = null;
            m_kbPlayer = null;
            EnableControls(true);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Checked)
            {                
                if (m_playThread == null)
                {                   
                    if (m_kbPlayer == null)
                    {
                        m_kbPlayer = new KeyBoardPlayer( m_iTargetPid, 
                                                         ckbCalc.Checked, 
                                                         ckbNote.Checked, 
                                                         cbxHtml.Checked,
                                                         combxCheckFile.SelectedIndex );
                        if (m_kbPlayer.TargetMainWnd == IntPtr.Zero)
                        {
                            btnStart.Checked = false;
                            m_kbPlayer = null;
                            return;
                        }
                    }
                    btnStart.Text = "Pause";
                    EnableControls(false);
                    m_kbPlayer.StoListFilePath = txbxEkbFile.Text;
                    m_kbPlayer.Speed = speedBar.Value;
                    m_playThread = new Thread(m_kbPlayer.Play);
                    m_playThread.Start();
                }
                else
                {
                    btnStart.Text = "Pause";
                    Monitor.Exit(m_kbPlayer);
                }
            }
            else
            {
                btnStart.Text = "Resume";
                Monitor.Enter(m_kbPlayer);
            } 
        }

        private void btnEkbFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "D:\\Program Files\\gtja\\JunhongYouPlus(NaturalVitality)\\T0002\\export\\";
            openFileDialog.Filter = "excel|*.xls|文本文件|*.txt|所有文件|*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txbxEkbFile.Text = openFileDialog.FileName;
            }
        }

        private void CSKB_Shown(object sender, EventArgs e)
        {
            if (!m_bRunByUser)
            {
                btnStart.Checked = true;
                btnStart_Click(sender, e);
            }
        }

        private void speedBar_ValueChanged(object sender, EventArgs e)
        {
            if (m_kbPlayer != null)
            {
                m_kbPlayer.Speed = speedBar.Value;
            }
        }       
    }
}
